// Package business 包含相关的API处理器
// @author 5title_
// @date 2025-04-01

package business

import (
	baseModel "5title_gin/internal/model"
	"5title_gin/internal/model/business/orders"
	"5title_gin/internal/service/business"
	"5title_gin/pkg/utils/http"
	"github.com/gin-gonic/gin"
	"strconv"
)

// OrdersAPI API
type OrdersAPI struct {
	service *business.OrdersService
}

// NewOrdersAPI 创建API
func NewOrdersAPI() *OrdersAPI {
	return &OrdersAPI{
		service: business.NewOrdersService(),
	}
}

// GetOrders 获取单个
// @Summary 获取单个
// @Description 根据ID获取详情
// @Tags 管理
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Success 200 {object} http.http{data=model.business.orders.Ordershttp}
// @Router /api/business/orders/{id} [get]
func (api *OrdersAPI) GetOrders(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		http.FailWithMsg("ID格式错误", c)
		return
	}

	data, err := api.service.GetOrdersById(uint(id))
	if err != nil {
		http.FailWithMsg(err.Error(), c)
		return
	}

	http.OkWithData(data, c)
}

// GetOrdersList 获取列表
// @Summary 获取列表
// @Description 分页获取列表
// @Tags 管理
// @Accept json
// @Produce json
// @Param page query int false "页码"
// @Param pageSize query int false "每页数量"
// @Success 200 {object} http.http{data=http.PageResult{list=[]model.business.orders.Ordershttp}}
// @Router /api/business/orders [get]
func (api *OrdersAPI) GetOrdersList(c *gin.Context) {
	var pageInfo baseModel.PageInfo
	// 参数已通过中间件绑定

	list, total, err := api.service.GetOrdersList(pageInfo)
	if err != nil {
		http.FailWithMsg(err.Error(), c)
		return
	}

	http.Ok(http.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// CreateOrders 创建
// @Summary 创建
// @Description 创建新的
// @Tags 管理
// @Accept json
// @Produce json
// @Param data body orders.Orders true "信息"
// @Success 200 {object} http.http
// @Router /api/business/orders [post]
func (api *OrdersAPI) CreateOrders(c *gin.Context) {
	var data orders.Orders
	// 参数已通过中间件绑定

	if err := api.service.CreateOrders(&data); err != nil {
		http.FailWithMsg(err.Error(), c)
		return
	}

	http.OkWithMsg("创建成功", c)
}

// UpdateOrders 更新
// @Summary 更新
// @Description 更新信息
// @Tags 管理
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Param data body orders.Orders true "信息"
// @Success 200 {object} http.http
// @Router /api/business/orders/{id} [put]
func (api *OrdersAPI) UpdateOrders(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		http.FailWithMsg("ID格式错误", c)
		return
	}

	var data orders.Orders
	// 参数已通过中间件绑定

	data.ID = uint(id)
	if err := api.service.UpdateOrders(&data); err != nil {
		http.FailWithMsg(err.Error(), c)
		return
	}

	http.OkWithMsg("更新成功", c)
}

// DeleteOrders 删除
// @Summary 删除
// @Description 删除指定ID的
// @Tags 管理
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Success 200 {object} http.http
// @Router /api/business/orders/{id} [delete]
func (api *OrdersAPI) DeleteOrders(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		http.FailWithMsg("ID格式错误", c)
		return
	}

	if err := api.service.DeleteOrders(uint(id)); err != nil {
		http.FailWithMsg(err.Error(), c)
		return
	}

	http.OkWithMsg("删除成功", c)
}
